package com.controller;

import com.common.api.ApiResult;
import com.entity.FlyTask;
import com.service.FlyTaskService;
import com.service.IFlyTaskService;
import com.websocket.MavlinkMessageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/flytask")
public class FlyTaskController {

    @Autowired
    private IFlyTaskService flyTaskService;

    @Autowired
    private MavlinkMessageHandler mavlinkMessageHandler;

    @PostMapping("/add")
    public ApiResult sendFlyRectangleByP2PCmd(@RequestBody FlyTask flyTask) {
//        System.out.println(flyTask.getAltitude());
//        System.out.println(flyTask.getSpeed());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        flyTask.setFlyStartDate(LocalDateTime.now());
        String timeId = simpleDateFormat.format(System.currentTimeMillis());
        flyTask.setTrackId(timeId);
        mavlinkMessageHandler.setTimeId(timeId);
        flyTaskService.save(flyTask);
        return ApiResult.success(flyTaskService.getById(flyTask).getId());
    }


    @GetMapping("/getAll")
    public ApiResult get() {
        return ApiResult.success(flyTaskService.list());
    }

    @GetMapping("/test")
    public ApiResult test() {
//        System.out.println("发送相应请求");
        return ApiResult.success("连接成功");
    }
}
